package com.wing.wcabinet.controller.app;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.CabinetCargoLane;
import com.wing.cabinet.model.response.CabinetCargoLaneVO;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.wcabinet.model.entity.*;
import com.wing.wcabinet.model.request.*;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.wcabinet.model.response.WcabCellInventoryInoutDetailVO;
import com.wing.wcabinet.service.WcabCellInventoryInoutDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-售刀柜上货/领取明细管理", tags = "售刀柜上货/领取明细管理")
@RestController("appWcabCellInventoryInoutDetailController")
@RequestMapping("wcabinet/app/wcabCellInventoryInoutDetail")
@Slf4j
public class WcabCellInventoryInoutDetailController {

    @Autowired
    private WcabCellInventoryInoutDetailService wcabCellInventoryInoutDetailService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "领用物料列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listOut")
    public PageResult<WcabCellInventoryInoutDetailVO> list(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        if ("user".equals(queryFlag)){
            params.put("creator", JwtUtils.getLoginUserId());
        }
        params.put("inoutType",WcabCellInventoryInout.InoutType.out.getValue());
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "领用记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/out/list")
    public PageResult<WcabCellInventoryInoutDetailVO> listOut(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        if ("user".equals(queryFlag)){
            params.put("creator", JwtUtils.getLoginUserId());
        }
        params.put("operationType",WcabCellInventoryInout.OperationType.out.getValue());
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "根据orderCode查询领用记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listByOrderCode")
    public PageResult<WcabCellInventoryInoutDetailVO> listByOrderCode(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
            ,String orderCode
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        params.put("orderCode",orderCode);
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "退料记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/return/list")
    public PageResult<WcabCellInventoryInoutDetailVO> listReturn(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        if ("user".equals(queryFlag)){
            params.put("creator", JwtUtils.getLoginUserId());
        }
        params.put("operationType",WcabCellInventoryInout.OperationType.returnIn.getValue());
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "维修记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/repair/list")
    public PageResult<WcabCellInventoryInoutDetailVO> listRepair(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        if ("user".equals(queryFlag)){
            params.put("creator", JwtUtils.getLoginUserId());
        }
        params.put("operationType",WcabCellInventoryInout.OperationType.repairIn.getValue());
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "报废记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/recycle/list")
    public PageResult<WcabCellInventoryInoutDetailVO> listRecycle(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        if ("user".equals(queryFlag)){
            params.put("creator", JwtUtils.getLoginUserId());
        }
        params.put("operationType",WcabCellInventoryInout.OperationType.recycleIn.getValue());
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "上货记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/load/list")
    public PageResult<WcabCellInventoryInoutDetailVO> listLoad(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        if ("user".equals(queryFlag)){
            params.put("creator", JwtUtils.getLoginUserId());
        }
        params.put("operationType",WcabCellInventoryInout.OperationType.load.getValue());
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "上货记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/unload/list")
    public PageResult<WcabCellInventoryInoutDetailVO> listUnload(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String cabCodes
            ,String queryFlag
    ) {
        Page<WcabCellInventoryInoutDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("queryFlag",queryFlag);
        if ("user".equals(queryFlag)){
            params.put("creator", JwtUtils.getLoginUserId());
        }
        params.put("operationType",WcabCellInventoryInout.OperationType.unload.getValue());
        Page<WcabCellInventoryInoutDetailVO> list = wcabCellInventoryInoutDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long wcabCellInventoryInoutDetailId) {
        WcabCellInventoryInoutDetailVO wcabCellInventoryInoutDetailVO = wcabCellInventoryInoutDetailService.get4appById(wcabCellInventoryInoutDetailId);
        if (wcabCellInventoryInoutDetailVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), wcabCellInventoryInoutDetailVO);
    }

    @PostMapping()
    public JsonResult add(@Validated WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm) {
        if (wcabCellInventoryInoutDetailService.add(wcabCellInventoryInoutDetailForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "售刀柜领取", notes = "领取")
    @PostMapping("/inventoryOut")
    public JsonResult inventoryOut(@RequestBody WcabCellInventoryInoutForm cellInventoryInoutForm){
        try{
            checkParams(cellInventoryInoutForm.getDetailList());
            WcabCellInventoryInout wcabCellInventoryInout= wcabCellInventoryInoutDetailService.inventoryOut(cellInventoryInoutForm.getDetailList(),cellInventoryInoutForm.getDeviceCode(), WcabCellInventoryInout.BusinessType.normal,WcabCellInventoryInout.OperationType.out,null);
            return JsonResult.success("领取成功",wcabCellInventoryInout);
        }catch (Exception e){
            log.error("cell inventory out fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "售刀柜领取是否有权限", notes = "领取")
    @PostMapping("/verifyPermissions")
    public JsonResult verifyPermissions(@RequestBody Map<String,String> map){
        //log.info("verify permission param:{}",map.get("skuCode")+"------"+map.get("cabCode"));
        //return wcabCellInventoryInoutDetailService.verifyPermissions(map.get("skuCode"),map.get("cabCode"));
        try{
            log.info("verify permission param:{}",map.get("skuCode")+"------"+map.get("cabCode"));
            return wcabCellInventoryInoutDetailService.verifyPermissions(map.get("skuCode"),map.get("cabCode"));
        }catch (Exception e){
            log.error("verify permission fail",e);
            return JsonResult.fail("权限判定异常",e.getMessage());
        }
    }

    /**
    @ApiOperation(value = "售刀柜清空", notes = "清空")
    @PostMapping("/empty/inventoryOut")
    public JsonResult inventoryOutEmpty(@RequestBody WcabCellInventoryInoutForm cellInventoryInoutForm){
        try{
            checkParams(cellInventoryInoutForm.getDetailList());
            WcabCellInventoryInout wcabCellInventoryInout= wcabCellInventoryInoutDetailService.inventoryOut(cellInventoryInoutForm.getDetailList(),cellInventoryInoutForm.getDeviceCode(), WcabCellInventoryInout.BusinessType.normal,WcabCellInventoryInout.OperationType.out,null);
            return JsonResult.success("领取成功",wcabCellInventoryInout);
        }catch (Exception e){
            log.error("cell inventory out fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }*/
    @ApiOperation(value = "售刀柜领取", notes = "领取")
    @PostMapping("/inventoryOutByPassword")
    public JsonResult inventoryOutByPassword(@RequestBody WcabCellInventoryInoutForm cellInventoryInoutForm){
        try{
            checkParams(cellInventoryInoutForm.getDetailList());
            WcabCellInventoryInout wcabCellInventoryInout=wcabCellInventoryInoutDetailService.inventoryOut(cellInventoryInoutForm.getDetailList(),cellInventoryInoutForm.getDeviceCode(), WcabCellInventoryInout.BusinessType.normal,WcabCellInventoryInout.OperationType.out,cellInventoryInoutForm.getStoresRequisitionId());
            return JsonResult.success("领取成功",wcabCellInventoryInout);
        }catch (Exception e){
            log.error("cell inventory out fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "存入", notes = "存维修")
    @PostMapping("/normal/inventoryIn")
    public JsonResult normalInventoryIn(@RequestBody WcabCellInventoryInoutForm cellInventoryInoutForm){
        try{
            checkParams(cellInventoryInoutForm.getDetailList());
            cellInventoryInoutForm.setBusinessType(WcabCellInventoryInout.BusinessType.normal);
            cellInventoryInoutForm.setOperationType(WcabCellInventoryInout.OperationType.load);
            wcabCellInventoryInoutDetailService.inventoryIn(cellInventoryInoutForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("repair inventory in fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "存维修", notes = "存维修")
    @PostMapping("/repair/inventoryIn")
    public JsonResult repairInventoryIn(@RequestBody WcabCellInventoryInoutForm cellInventoryInoutForm){
        try{
            checkParams(cellInventoryInoutForm.getDetailList());
            cellInventoryInoutForm.setBusinessType(WcabCellInventoryInout.BusinessType.repair);
            cellInventoryInoutForm.setOperationType(WcabCellInventoryInout.OperationType.repairIn);
            wcabCellInventoryInoutDetailService.inventoryIn(cellInventoryInoutForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("repair inventory in fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "存报废", notes = "存维修")
    @PostMapping("/recycle/inventoryIn")
    public JsonResult recycleInventoryIn(@RequestBody WcabCellInventoryInoutForm cellInventoryInoutForm){
        try{
            checkParams(cellInventoryInoutForm.getDetailList());
            cellInventoryInoutForm.setBusinessType(WcabCellInventoryInout.BusinessType.recycle);
            cellInventoryInoutForm.setOperationType(WcabCellInventoryInout.OperationType.recycleIn);
            wcabCellInventoryInoutDetailService.inventoryIn(cellInventoryInoutForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("recycle inventory in fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "退货", notes = "退货")
    @PostMapping("/return/inventoryIn")
    public JsonResult returnInventoryIn(@RequestBody WcabCellInventoryInoutForm cellInventoryInoutForm){
        try{
            checkParams(cellInventoryInoutForm.getDetailList());
            cellInventoryInoutForm.setBusinessType(WcabCellInventoryInout.BusinessType.normal);
            cellInventoryInoutForm.setOperationType(WcabCellInventoryInout.OperationType.returnIn);
            wcabCellInventoryInoutDetailService.inventoryIn(cellInventoryInoutForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("recycle inventory in fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "上货", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/load")
    public JsonResult wcabLoad(@Validated(WcabCellLoadDetailForm.Add.class) @RequestBody WcabCellLoadDetailForm wcabCellLoadDetailForm) {
        try{
            checkParams(wcabCellLoadDetailForm);
            log.info("load param:{}", JSON.toJSONString(wcabCellLoadDetailForm));
            wcabCellLoadDetailForm.setType(WcabCellLoad.Type.selfBatchLoadSimple);
            wcabCellLoadDetailForm.setOperationType(WcabCellLoad.OperationType.load);
            wcabCellInventoryInoutDetailService.inventoryIn(wcabCellLoadDetailForm);
            return JsonResult.success("上货成功");
        }catch (Exception e){
            log.error("receive load fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "补货", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/loadAdd")
    public JsonResult wcabLoadAdd(@Validated(WcabCellLoadDetailForm.Add.class) @RequestBody WcabCellLoadDetailForm wcabCellLoadDetailForm) {
        try{
            checkParams(wcabCellLoadDetailForm);
            log.info("load param:{}", JSON.toJSONString(wcabCellLoadDetailForm));
            wcabCellLoadDetailForm.setType(WcabCellLoad.Type.selfBatchLoadSimple);
            wcabCellLoadDetailForm.setOperationType(WcabCellLoad.OperationType.supplyIn);
            wcabCellInventoryInoutDetailService.inventoryIn(wcabCellLoadDetailForm);
            return JsonResult.success("上货成功");
        }catch (Exception e){
            log.error("receive load fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "下货", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/unload")
    public JsonResult wcabUnload(@Validated(WcabCellLoadDetailForm.Add.class)@RequestBody WcabCellLoadDetailForm wcabCellLoadDetailForm) {
        try{
            checkParams(wcabCellLoadDetailForm);
            log.info("unload param:{}", JSON.toJSONString(wcabCellLoadDetailForm));
            wcabCellLoadDetailForm.setType(WcabCellLoad.Type.selfBatchUnloadSimple);
            wcabCellLoadDetailForm.setOperationType(WcabCellLoad.OperationType.unload);
            wcabCellInventoryInoutDetailService.inventoryOut(wcabCellLoadDetailForm);
            return JsonResult.success("下货成功");
        }catch (Exception e){
            log.error("receive load fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "下货", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/empty/unload")
    public JsonResult wcabUnloadEmpty(@Validated(WcabCellLoadDetailForm.Add.class)@RequestBody WcabCellLoadDetailForm wcabCellLoadDetailForm) {
        try{
//            checkParams(wcabCellLoadDetailForm);
            log.info("unload param:{}", JSON.toJSONString(wcabCellLoadDetailForm));
            wcabCellLoadDetailForm.setType(WcabCellLoad.Type.selfBatchUnloadSimple);
            wcabCellLoadDetailForm.setOperationType(WcabCellLoad.OperationType.unload);
            wcabCellInventoryInoutDetailService.inventoryOut4Empty(wcabCellLoadDetailForm);
            return JsonResult.success("下货成功");
        }catch (Exception e){
            log.error("receive load fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "获取成品货道", notes = "")
    @PostMapping("/normal/getSpaces")
    public JsonResult getNormalSpaces(@RequestBody WcabUseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("get storage Spaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoLane.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoLane.SpaceType.normal.getValue());
            List<CabinetCargoLaneVO> spaceVO = wcabCellInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), WcabCellInventoryInout.BusinessType.normal,spaceTypes,useInventoryInoutDetailForm.getWeight());
            return JsonResult.success(spaceVO);
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取成品货道", notes = "")
    @PostMapping("/normal/getOptimalSpaces")
    public JsonResult getOptimalSpaces(@RequestBody WcabUseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("get storage Spaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoLane.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoLane.SpaceType.normal.getValue());
            List<CabinetCargoLaneVO> spaceVOs = wcabCellInventoryInoutDetailService.getOptimalSpaces(useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), WcabCellInventoryInout.BusinessType.normal,spaceTypes,useInventoryInoutDetailForm.getWeight(),useInventoryInoutDetailForm.getQuantity());
            if (spaceVOs.size()==0){
                return JsonResult.fail("未找到足够库存的货道");
            }
            return JsonResult.success(spaceVOs);
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取退货成品货道", notes = "")
    @PostMapping("/normal/getReturnOptimalSpaces")
    public JsonResult getReturnOptimalSpaces(@RequestBody WcabUseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("get storage Spaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoLane.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoLane.SpaceType.normal.getValue());
//            List<CabinetCargoLaneVO> spaceVOs = wcabCellInventoryInoutDetailService.getReturnOptimalSpaces(useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), WcabCellInventoryInout.BusinessType.normal,spaceTypes,useInventoryInoutDetailForm.getWeight(),useInventoryInoutDetailForm.getQuantity());
            List<CabinetCargoLaneVO> spaceVOs = wcabCellInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), WcabCellInventoryInout.BusinessType.normal,spaceTypes,useInventoryInoutDetailForm.getWeight());
            if (spaceVOs.size()==0){
                return JsonResult.fail("未找到足够库存的货道");
            }
            return JsonResult.success(spaceVOs);
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "获取维修货道", notes = "")
    @PostMapping("/repair/getSpaces")
    public JsonResult getRepairSpaces(@RequestBody WcabUseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("get storage Spaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoLane.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoLane.SpaceType.repair.getValue());
            List<CabinetCargoLaneVO> spaceVO = wcabCellInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), WcabCellInventoryInout.BusinessType.repair,spaceTypes,useInventoryInoutDetailForm.getWeight());
            return JsonResult.success(spaceVO);
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }


    @ApiOperation(value = "获取报废货道", notes = "")
    @PostMapping("/recycle/getSpaces")
    public JsonResult getStorageUserSpaces(@RequestBody WcabUseInventoryInoutDetailForm useInventoryInoutDetailForm) {
        try{
            log.info("get storage Spaces param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
//            CabinetCargoSpaceVO spaceVO = storageInventoryInoutDetailService.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
            List<Integer> spaceTypes=new ArrayList<>();
            spaceTypes.add(CabinetCargoLane.SpaceType.common.getValue());
            spaceTypes.add(CabinetCargoLane.SpaceType.recycle.getValue());
            List<CabinetCargoLaneVO> spaceVO = wcabCellInventoryInoutDetailService.getSpaces(useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), WcabCellInventoryInout.BusinessType.recycle,spaceTypes,useInventoryInoutDetailForm.getWeight());
            return JsonResult.success(spaceVO);
        }catch (Exception e){
            log.error("get storage space fail",e);
            return JsonResult.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "盘验（柜子面板）", notes = "盘验")
    @PostMapping("/check4Cab")
    public JsonResult check4Cab(@RequestBody WcabCellInventoryInoutForm wcabCellInventoryInoutForm){
        try{
            log.info("check params:{}",JSON.toJSONString(wcabCellInventoryInoutForm));
            wcabCellInventoryInoutDetailService.check(wcabCellInventoryInoutForm);
            return JsonResult.success();
        }catch (Exception e){
            log.error("check fail",e);
            return JsonResult.fail(e.getMessage());
        }
    }

    void checkParams(List<WcabCellInventoryInoutDetailForm> detailList){
        if (CollectionUtil.isEmpty(detailList)){
            throw new BusinessException("缺少明细参数");
        }
        for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:detailList){
            if (wcabCellInventoryInoutDetailForm.getWeight()==null||wcabCellInventoryInoutDetailForm.getWeight().doubleValue()<=0||
                    wcabCellInventoryInoutDetailForm.getQuantity()==null||wcabCellInventoryInoutDetailForm.getQuantity()<=0){
                throw new BusinessException("重量/数量参数异常");
            }
        }
    }
    void checkParams(WcabCellLoadDetailForm wcabCellLoadDetailForm){
        if(wcabCellLoadDetailForm.getQuantity()==null||wcabCellLoadDetailForm.getQuantity()<=0||
        wcabCellLoadDetailForm.getWeight()==null||wcabCellLoadDetailForm.getWeight().doubleValue()<=0){
            throw new BusinessException("重量/数量参数异常");
        }

    }
}
